<?php
 /*****************************************************************************
 * viewInvoice.php                                                            *
 * Last Modified: 2007-06-06                                                  *
 *                                                                            *
 * viewInvoice.php outputs the data stored in the invoice record selected     *
 * from invoices.php. Allows user to print the record or download it as an    *
 * Acrobat (.pdf) file.                                                       *
 *                                                                            *
 * BBG_Billing, a PHP application using MySQL for creating and maintaining a  *
 * contacts and invoices database.                                            *
 *                                                                            *
 * @copyright Copyright (C) 2007, Bugs Bee Gone Computer Services             *
 * @owner     Daniel Barnett (Bugs Bee Gone Computer Services)                *
 * @author    Daniel Barnett <dbarnett@bugsbeegone.com>                       *
 * @website   http://www.bugsbeegone.com                                      *
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License *
 * @package   BBG_Billing                                                     *
 * @name      viewInvoice.php                                                 *
 * @version   1.0.0                                                           *
 * @uses      Jpmaster77's Login Script (Written by: Jpmaster77, 2004-08-19)  *
 *             ->(http://www.evolt.org/PHP-Login-System-with-Admin-Features/) *
 *                                                                            *
 * This file is part of the "BBG_Billing" PHP application.                    *
 *                                                                            *
 * BBG_Billing is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * BBG_Billing is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with BBG_Billing; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 *****************************************************************************/


    // Require session.php for user access control
    require "include/session.php";

    // If user is not logged in, redirect to main.php
    if(!$session->logged_in){
        header("Location: main.php");
        exit();
    }

    // Require config.inc.php for configuration variables
    require_once "config.inc.php";

    // Connect to MySQL
    mysql_connect($path, $username, $password);
    // Select database, if error die with error message
    @mysql_select_db($database) or die("Unable to select database.<br />".mysql_error());

    // Get invoiceNum value from URL
    $invoiceNum=$_GET['invoiceNum'];
    // Get buttonsVis value from URL
    $buttonsVis=$_GET['buttons'];

    // If buttonsVis is NULL or TRUE...
    if($buttonsVis == "" || $buttonsVis == "true" || $buttonsVis == "TRUE") {
        // buttonsVis is TRUE
        $buttonsVis = "true";
    // All other buttonsVis values...
    } else {
        // buttonsVis is FALSE
        $buttonsVis = "false";
    }

    // If invoiceNum is NULL...
    if($invoiceNum == "") {
        // Die with error message
        die('Cannot retrieve the invoice without the Invoice Number. Please enter the Invoice Number and <a href="invoices.php">try again</a>.');
    // If invoiceNum is not NULL...
    } else {
        // Define MySQL query to select the invoice data
        $query = "SELECT * FROM tblInvoices WHERE invoiceNum='" . $invoiceNum . "' LIMIT 1";

        // Execute the query, if error die with error message
        $result = mysql_query($query) or die(mysql_error());

        // Determine number of rows returned by the MySQL query
        $num = mysql_numrows($result);

        // If no rows were returned by the MySQL query...
        if($num == 0) {
            // Die with error message
            die('Record not found. Please check the Invoice Number and <a href="invoices.php">try again</a>.');
        // If rows were returned by the MySQL query
        } else {
            // Define data variables for invoice data
            $key = mysql_result($result,0,"key");
            $date = mysql_result($result,0,"date");
            $invoiceNum = mysql_result($result,0,"invoiceNum");
            $customerID = mysql_result($result,0,"customerID");
			
            $item1Qty = mysql_result($result,0,'item1Qty');
            $item2Qty = mysql_result($result,0,'item2Qty');
            $item3Qty = mysql_result($result,0,'item3Qty');
            $item4Qty = mysql_result($result,0,'item4Qty');
            $item5Qty = mysql_result($result,0,'item5Qty');
            $item6Qty = mysql_result($result,0,'item6Qty');
            $item7Qty = mysql_result($result,0,'item7Qty');
            $item8Qty = mysql_result($result,0,'item8Qty');
            $item9Qty = mysql_result($result,0,'item9Qty');
            $item10Qty = mysql_result($result,0,'item10Qty');

            $item1Name = mysql_result($result,0,'item1Name');
            $item2Name = mysql_result($result,0,'item2Name');
            $item3Name = mysql_result($result,0,'item3Name');
            $item4Name = mysql_result($result,0,'item4Name');
            $item5Name = mysql_result($result,0,'item5Name');
            $item6Name = mysql_result($result,0,'item6Name');
            $item7Name = mysql_result($result,0,'item7Name');
            $item8Name = mysql_result($result,0,'item8Name');
            $item9Name = mysql_result($result,0,'item9Name');
            $item10Name = mysql_result($result,0,'item10Name');

            $item1Rate = mysql_result($result,0,'item1Rate');
            $item2Rate = mysql_result($result,0,'item2Rate');
            $item3Rate = mysql_result($result,0,'item3Rate');
            $item4Rate = mysql_result($result,0,'item4Rate');
            $item5Rate = mysql_result($result,0,'item5Rate');
            $item6Rate = mysql_result($result,0,'item6Rate');
            $item7Rate = mysql_result($result,0,'item7Rate');
            $item8Rate = mysql_result($result,0,'item8Rate');
            $item9Rate = mysql_result($result,0,'item9Rate');
            $item10Rate = mysql_result($result,0,'item10Rate');

            $item1Total = mysql_result($result,0,'item1Total');
            $item2Total = mysql_result($result,0,'item2Total');
            $item3Total = mysql_result($result,0,'item3Total');
            $item4Total = mysql_result($result,0,'item4Total');
            $item5Total = mysql_result($result,0,'item5Total');
            $item6Total = mysql_result($result,0,'item6Total');
            $item7Total = mysql_result($result,0,'item7Total');
            $item8Total = mysql_result($result,0,'item8Total');
            $item9Total = mysql_result($result,0,'item9Total');
            $item10Total = mysql_result($result,0,'item10Total');

            $discount = mysql_result($result,0,'discount');
            $tax = mysql_result($result,0,'tax');
            $subTotal = mysql_result($result,0,'subTotal');
            $total = mysql_result($result,0,'total');
            $notes = mysql_result($result,0,'notes');

            // Close the connection
            mysql_close();

            // Connect to MySQL
            mysql_connect($path, $username, $password);
            // Select the database, if error die with error message
            @mysql_select_db($database) or die("Unable to select database.<br />" . mysql_error());

            // Define MySQL query for selecting the customer data
            $query2 = "SELECT * FROM tblContacts WHERE customerID='" . $customerID . "' LIMIT 1";

            // Execute the MySQL query, if error die with error message
            $result2 = mysql_query($query2) or die(mysql_error());

            // Determine the number of rows returned by the MySQL query
            $num2 = mysql_numrows($result2);

            // If no rows were returned by the MySQL query...
		if($num2 == 0) {
                // Die with error message
                die('Error retrieving the customer data. Please check the Customer Number and <a href="invoices.php">try again</a>.');
            // If rows were returned by the MySQL query...
            } else {
                // Define data variables for the customer data
                $company = mysql_result($result2,0,"company");
                $firstName = mysql_result($result2,0,"firstName");
                $lastName = mysql_result($result2,0,"lastName");
                $address1 = mysql_result($result2,0,"address1");
                $address2 = mysql_result($result2,0,"address2");
                $city = mysql_result($result2,0,"city");
                $state = mysql_result($result2,0,"state");
                $zipCode = mysql_result($result2,0,"zipCode");
                $phone1 = mysql_result($result2,0,"phone1");
                $phone2 = mysql_result($result2,0,"phone2");
                $fax = mysql_result($result2,0,"fax");
                $email = mysql_result($result2,0,"email");
                $webSite = mysql_result($result2,0,"webSite");
            }

            // Close the connection
            mysql_close();
        }
    }

echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";

echo '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
	echo '<head>' . "\n";
		echo '<meta name="ROBOTS" content="NOODP, NOFOLLOW" />' . "\n";
		echo '<meta name="GOOGLEBOT" content="NOODP, NOFOLLOW" />' . "\n";
		echo '<title>Bugs Bee Gone - Billing - Invoice Number: ' . $invoiceNum . ' (' . $date . ')</title>' . "\n";
		echo '<meta name="description" content="Bugs Bee Gone - Billing - View Invoice" />' . "\n";
		echo '<link rel="shortcut icon" href="" />' . "\n";
		echo '<link href="invoice.css" type="text/css" rel="stylesheet" />' . "\n";

		echo '<script type="text/javascript" src="func.inc.js"></script>' . "\n";

		echo '<script type="text/javascript" src="print_page.js"></script>' . "\n";
		echo '<script type="text/javascript">' . "\n";
		echo 'function printPage() {' . "\n";
		echo 'createPage(document.getElementById("invoice").innerHTML, "http://www.bugsbeegone.com", "Bugs Bee Gone - Billing - ' . $invoiceNum . '", "");' . "\n";
		echo '}' . "\n";

		echo 'function createPDF() {' . "\n";
		echo 'newPDF("http://www.bugsbeegone.com/billing/pdfInvoice.php?invoiceNum=' . $invoiceNum . '", "invoice_' . $invoiceNum . '_' . $date . '");' . "\n";
		echo '}' . "\n";
		echo '</script>' . "\n";
	echo '</head>' . "\n";
?>

	<body>

	<div id="invoice">
		<table width="700" border="0" align="center">
		<tr>
			<td valign="bottom" width="250" style="word-wrap: break-word;">
				<img  src="bbg_logo.gif" width="192" height="153" alt="Bugs Bee Gone logo" />
			</td>

			<td valign="bottom" width="550" style="word-wrap:break-word; text-align:left;">
				Bugs Bee Gone<br />
				PO Box 923<br />
				Mexico, Missouri 65265<br />
				573-721-3245<br />
				sales@bugsbeegone.com<br />
				http://www.bugsbeegone.com
			</td>
		</tr>

		<tr>
			<td valign="bottom" width="100%" colspan="2" style="font-weight:bold; font-size:12pt; text-align:right;">
				Invoice #: <?php echo $invoiceNum; ?>
			</td>
		</tr>
		
		<tr>
			<td valign="bottom" width="100%" colspan="2" style="font-weight:normal; font-size:10pt; text-align:right;">
				<?php echo $date; ?><br />
				<hr />
			</td>
		</tr>

		<tr>
			<td valign="top" width="100%" colspan="2" style="font-weight:bold; font-size:10pt; text-align:left;">
				Bill to:
			</td>
		</tr>

		<tr>
			<td valign="top" width="100%" colspan="2" style="font-weight:normal; font-size:10pt; text-align:left;">
                        <?php
                            // If company is not NULL...
                            if($company != "") {
                                // Output company then the contact's name
                                echo '<b>' . $company . '</b><br />' . "\n";
                                echo $firstName . ' ' . $lastName . '<br />' . "\n";
                            // If company is NULL...
                            } else {
                                // Output the contact's name
                                echo '<b>' . $firstName . ' ' . $lastName . '</b><br />' . "\n";
                            }

                            echo $address1 . '<br />' . "\n";

                            // If address2 is not NULL, output address2
                            if($address2 != "") {
                                echo $address2 . '<br />' . "\n";
                            }

                            echo $city . ', ' . $state . ' ' . $zipCode . '<br />' . "\n";

                            echo 'Phone: ' . $phone1 . '<br />' . "\n";

                            // If phone2 is not NULL, output phone2
                            if($phone2 != "") {
                                echo 'Phone: ' . $phone2 . '<br />' . "\n";
                            }

                            // If fax is not NULL, output fax
                            if($fax != "") {
                                echo 'Fax: ' . $fax . '<br />' . "\n";
                            }

                            // If email is not NULL, output email
                            if($email != "") {
                                echo $email . '<br />' . "\n";
                            }
				?>
				<hr />
			</td>
		</tr>

		<tr>
			<td valign="top" width="100%" colspan="2" style="font-weight:normal; font-size:10pt; text-align:left;">
				<!- TERMS AND CONDITIONS OF THE INVOICE //-->
				<b>Terms:</b> Balance due within 30 days of invoice date. Overdue accounts will be subject to a 
				service charge of 10% per month (30 days).

				<hr />
			</td>
		</tr>

		<tr>
			<td valign="top" width="100%" colspan="2" style="text-align:left;">
				<table width="100%" border="0">
					<!-- ITEM ROW HEADINGS //-->
					<tr>
						<th valign="top" width="100" class="invoice">Quantity</th>
						<th valign="top" width="400" class="invoice">Item</th>
						<th valign="top" width="100" class="invoice" style="text-align:right;">Rate</th>
						<th valign="top" width="100" class="invoice" style="text-align:right;">Total</th>
					</tr>

					<!-- ITEM 1 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item1Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item1Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item1Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item1Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 2 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item2Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item2Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item2Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item2Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 3 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item3Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item3Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item3Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item3Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 4 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item4Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item4Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item4Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item4Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 5 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item5Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item5Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item5Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item5Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 6 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item6Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item6Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item6Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item6Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 7 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item7Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item7Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item7Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item7Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 8 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item8Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item8Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item8Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item8Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 9 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item9Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item9Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item9Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item9Total; ?>
						</td>
					</tr>

					<tr><td colspan="4" style="font-size:6pt;"> </td></tr>

					<!-- ITEM 10 //-->
					<tr>
						<td valign="top" width="100">
							<?php echo $item10Qty; ?>
						</td>
						<td valign="top" width="400px">
							<?php echo $item10Name; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item10Rate; ?>
						</td>
						<td valign="top" width="100" style="text-align:right;">
							<?php echo $item10Total; ?>
						</td>
					</tr>

					<!-- DIVIDER LINE //-->
					<tr>
						<td valign="middle" width="100%" colspan="4"><hr /></td>
					</tr>

					<!-- SUBTOTAL //-->
					<tr>
						<td valign="top" colspan="3" style="text-align:right">
							Subtotal:
						</td>
						<td valign="top" style="text-align:right;">
							<?php echo $subTotal; ?>
						</td>
					</tr>

					<!-- DISCOUNTS //-->
					<tr>
						<td valign="top" colspan="3" style="text-align:right">
							Discounts:
						</td>
						<td valign="top" style="text-align:right;">
							- <?php echo $discount; ?>
						</td>
					</tr>

					<!-- TAXES //-->
					<tr>
						<td valign="top" colspan="3" style="text-align:right">
							Tax:
						</td>
						<td valign="top" style="text-align:right;">
							+ <?php echo $tax; ?>
						</td>
					</tr>

					<!-- DIVIDER LINE //-->
					<tr>
						<td valign="middle" width="100%" colspan="4"><hr /></td>
					</tr>

					<!-- TOTAL BALANCE DUE //-->
					<tr>
						<td valign="top" colspan="3" style="text-align:right; font-weight:bold;">
							Balance Due:
						</td>
						<td valign="top" style="text-align:right; font-weight:bold;">
							$ <?php echo $total; ?>
						</td>
					</tr>

					<!-- DIVIDER LINE //-->
					<tr>
						<td valign="middle" width="100%" colspan="4"><hr /></td>
					</tr>

                                <?php
                                    // If notes is not NULL...
                                    if($notes != "") {
                                        echo '<!-- INVOICE NOTES //-->' . "\n";
                                        echo '<tr>' . "\n";
                                            echo '<td valign="top" width="100%" colspan="4" style="text-align:left; font-weight:normal; font-size:10pt;">' . "\n";
                                                echo '<b><u>Notes:</u></b><br />' . "\n";
                                                echo $notes . "\n";
                                            echo '</td>' . "\n";
                                        echo '</tr>' . "\n";

                                        echo '<!-- DIVIDER LINE //-->' . "\n";
                                        echo '<tr>' . "\n";
                                            echo '<td valign="middle" width="100%" colspan="4"><hr /></td>' . "\n";
                                        echo '</tr>' . "\n";
                                    }
                                ?>

				</table>
			</td>
		</tr>
		</table>
	</div>

    <?php
        // If buttonsVis is TRUE...
        if($buttonsVis == "true") {
            // Make the buttons visible
            echo '<div id="buttons" style="text-align: center; visibility: visible;">' . "\n";
        // If buttonsVis is FALSE...
        } else {
            // Hide the buttons
            echo '<div id="buttons" style="text-align: center; visibility: hidden;">' . "\n";
        }
    ?>

		<input type="button" onclick="printPage()" value="Print" name="printButton" /> <input type="button" onclick="createPDF()" value="Download" /> <input type="button" onclick="cancel(this)" location="invoices.php" value="Cancel" /></div>

	</body>
</html>
